home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Deutsche Edition 1
/
Deutsche Edition 1.iso
/
amok
/
011-020
/
amok16
/
memsystem
/
memsystem.mod
< prev
next >
Wrap
Text File
|
1993-11-04
|
4KB
|
144 lines
(**********************************************************************
:Program. MemSystem.mod
:Contents. convenient memory allocation procedures
:Author. Nicolas Benezan [bne]
:Address. Postwiesenstr. 2, D7000 Stuttgart 60
:Phone. 711/333679
:Copyright. Public Domain
:Language. Modula-2
:Translator. M2Amiga AMSoft 3.2d
:Imports. ErrorReq,TaskMemory [bne]
:History. V1.0b [bne] 17.Jun.88 (pre-version, private)
:History. V1.1e [bne] 28.Oct.88 (Bug corrected)
:History. V1.2b [bne] 27.Jan.89 (ErrorReq items excluded)
:History. V1.3b [bne] 04.Mar.89 (+ NoCareDeallocate, Levels)
:Update. [bne] 27.Jan.89 adaptions for m2cV3.2d
**********************************************************************)
IMPLEMENTATION MODULE MemSystem;
FROM SYSTEM IMPORT ADR,ADDRESS,BYTE,CAST;
FROM Exec IMPORT MemReqSet,MemReqs,Forbid,Permit,AvailMem,UByte,
Remove,FindTask,MemListPtr,TaskPtr,FreeEntry;
FROM TaskMemory IMPORT AllocTaskMem,DeallocTaskMem,CHIP,ANY;
FROM ErrorReq IMPORT YesNoRequest,RETRY,CANCEL,ABORT,ExitQuiet;
FROM Arts IMPORT Assert;
CONST StdMinMem=20*1024;
StdHysteresis=30*1024;
(* Messages *)
Warning= "Low memory warning";
TwiceFreed= "can't Free() free Memory";
TYPE LevelKey=LONGINT;
PROCEDURE Alloc(VAR Pointer:ADDRESS;Size:LONGINT;Reqs:MemReqSet;ExitIfFails:BOOLEAN);
VAR Retry:BOOLEAN;
PROCEDURE LowMemWarning(VAR Answer:BOOLEAN);
BEGIN
IF ExitIfFails THEN
Answer:=YesNoRequest(ADR(Warning),ADR(RETRY),ADR(ABORT));
IF NOT Answer THEN
ExitQuiet
(* procedure will never return *)
END;
ELSE
Answer:=YesNoRequest(ADR(Warning),ADR(RETRY),ADR(CANCEL));
END;
END LowMemWarning;
BEGIN
REPEAT
Forbid;
Pointer:=AllocTaskMem(Size,Reqs);
IF Pointer=NIL THEN
Permit;
LowMemWarning(Retry);
ELSIF AvailMem(MemReqSet{chip,largest})<minMemory THEN
DeallocTaskMem(Pointer);
Permit;
LowMemWarning(Retry);
ELSE
Permit;
END;
UNTIL (Pointer#NIL)OR NOT Retry;
END Alloc;
PROCEDURE Allocate(VAR Pointer:ADDRESS;Size:LONGINT);
BEGIN
Alloc(Pointer,Size,ANY,FALSE);
END Allocate;
PROCEDURE AllocMem(VAR Pointer:ADDRESS;Size:LONGINT;Chip:BOOLEAN);
BEGIN
IF Chip THEN
Alloc(Pointer,Size,CHIP,FALSE);
ELSE
Alloc(Pointer,Size,ANY,FALSE);
END;
END AllocMem;
PROCEDURE Deallocate(VAR Pointer:ADDRESS);
BEGIN
DeallocTaskMem(Pointer);
Assert(Pointer=NIL,ADR(TwiceFreed));
END Deallocate;
PROCEDURE NoCareAllocate(VAR Pointer:ADDRESS;Size:LONGINT);
BEGIN
Alloc(Pointer,Size,ANY,TRUE);
END NoCareAllocate;
PROCEDURE NoCareAllocMem(VAR Pointer:ADDRESS;Size:LONGINT;Chip:BOOLEAN);
BEGIN
IF Chip THEN
Alloc(Pointer,Size,CHIP,TRUE);
ELSE
Alloc(Pointer,Size,ANY,TRUE);
END;
END NoCareAllocMem;
PROCEDURE NoCareDeallocate(VAR Pointer:ADDRESS);
BEGIN
IF Pointer#NIL THEN
DeallocTaskMem(Pointer);
END;
END NoCareDeallocate;
PROCEDURE EnterLevel(VAR Level:LevelKey);
VAR Task:TaskPtr;
BEGIN
Task:=FindTask(NIL);
INC(CAST(UByte,Task^.memEntry.pad));
Level:=LONGINT(CAST(UByte,Task^.memEntry.pad));
END EnterLevel;
PROCEDURE ExitLevel(VAR Level:LevelKey);
VAR Task:TaskPtr;
Entry:MemListPtr;
BEGIN
Task:=FindTask(NIL);
WITH Task^.memEntry DO
IF Level<=LONGINT(CAST(UByte,pad)) THEN
pad:=CAST(BYTE,UByte(Level-1));
LOOP
Entry:=ADDRESS(head);
IF Entry^.node.succ=NIL THEN
EXIT
ELSIF LONGINT(CAST(UByte,Entry^.node.pri))<Level THEN
EXIT
END;
Remove(head);
FreeEntry(Entry);
END; (* loop *)
END; (* loop *)
END; (* with *)
END ExitLevel;
END MemSystem.